package thu.infinity.commons;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.thrift.TException;

import thu.infinity.thrift.Attr;
import thu.infinity.thrift.DirEntry;
import thu.infinity.thrift.MyBinary;
import thu.infinity.thrift.UserToken;
import thu.infinity.thrift.Aggregator.Iface;
import thu.infinity.facebook.FacebookUser;
import thu.infinity.facebook.FacebookConn;
import thu.infinity.facebook.LogInOutTask;
import thu.infinity.facebook.Util;

public class AggregatorHandler implements Iface {
	// From shared data
	public Map<String, FacebookUser> fbAllUser;
	public Map<String, Integer> userNotiMonitor;
	
	//public CacheManager cacheManager;
	//public LogManager logManager;
	
	//public LinkedList<FacebookUploadReq> uploadQueue;
	
	TaskScheduler queryScheduler;
	ThreadPoolExecutor cacheThreadPool;
	
	public AggregatorHandler() throws FileNotFoundException {
		File test = Util.newFile(Para.CacheRoot + "TestingLog.txt");
		PrintStream out = new PrintStream(new FileOutputStream(test));
		System.setOut(out);
		fbAllUser = new HashMap<String, FacebookUser>();
		userNotiMonitor = new HashMap<String, Integer>();
		
		queryScheduler = new TaskScheduler();
		queryScheduler.start();
		
		cacheThreadPool = new ThreadPoolExecutor(Para.CACHE_THREADPOOL_SIZE, 
				Para.CACHE_THREADPOOL_MAXSIZE, Para.CACHE_THREADPOOL_KEEPALIVETIME, 
				Para.CACHE_THREADPOOL_TIMEUNIT, new LinkedBlockingQueue<Runnable>(),
				new ThreadPoolExecutor.CallerRunsPolicy());
	}
	
	public TaskScheduler getQueryScheduler() {
		return queryScheduler;
	}
	
	public ThreadPoolExecutor getCacheThreadPool() {
		return cacheThreadPool;
	}
	
	
	
	@Override
	public String backend_new_write(UserToken token, long size)
			throws TException {
		/*
		System.out.println(getCurrentMethodName() + " " + token.uid + " " + size) ;
		long lid = sharedData.cacheManager.getNextCounter(token.uid);
		String ret = "" + lid;
		
		Attr attr = new Attr();
		attr.size = size;
		
		String fn = token.uid + "/" + ret;
		sharedData.cacheManager.initFile(fn, attr);
		
		System.out.println(getCurrentMethodName() + " File name " + fn);
		
		return fn;*/
		return null;
	}

	@Override
	public int backend_log_upload(UserToken token, String path, ByteBuffer buf,
			long offset, long size) throws TException {
		/*System.out.println(getCurrentMethodName() + ": " + path + " (+"
				+ offset + "," + size + ")");
		
		sharedData.logManager.write(token.uid, path, offset, size, buf);*/
		return 0;
	}

	@Override
	public long backend_write(UserToken token, String path, ByteBuffer buf,
			long offset, long size) throws TException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void backend_setAttr(UserToken token, String path, Attr attr)
			throws TException {
		// TODO Auto-generated method stub

	}

	@Override
	public MyBinary backend_read(UserToken token, String path, long offset,
			long size) throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MyBinary backend_prefetch(UserToken token, String path, long offset,
			long size) throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Attr backend_getAttr(UserToken token, String path) throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DirEntry> backend_readDir(UserToken token, String path)
			throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void backend_login(String uid, String token, String serviceName)
			throws TException {
		System.out.println("Proxy backend_login: " + uid + " " + token);
		User user = new User(uid, token, serviceName);
		Task task = new LogInOutTask(user, Para.LOG_IN, this);
		queryScheduler.AddTask(task);
	}

	@Override
	public void backend_logout(String uid, String token, String serviceName)
			throws TException {
		// TODO Auto-generated method stub

	}

	@Override
	public List<DirEntry> backend_pull(UserToken token, long time)
			throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DirEntry> backend_push(UserToken token, long time, long waitTime)
			throws TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DirEntry> backend_resync(List<DirEntry> localList)
			throws TException {
		// TODO Auto-generated method stub
		return null;
	}
}
